package com.ruoyi.project.mall.service.impl;

import cc.openkit.common.KitUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.bootdo.PageHelperUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.mall.dao.MallCompanyUserDao;
import com.ruoyi.project.mall.dao.OrderReviewDao;
import com.ruoyi.project.mall.domain.MallCompanyUserDO;
import com.ruoyi.project.mall.domain.OrderReviewDO;
import com.ruoyi.project.mall.service.OrderReviewService;
import com.ruoyi.project.wx.dao.MemberUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class OrderReviewServiceImpl implements OrderReviewService {
	@Autowired
	private OrderReviewDao orderReviewDao;
	@Autowired
	private MallCompanyUserDao mallCompanyUserDao;
	@Autowired
	private MemberUserDao memberUserDao;
	
	@Override
	public OrderReviewDO get(String id){
		return orderReviewDao.get(id);
	}
	
	@Override
	public List<OrderReviewDO> list(Map<String, Object> map){
		return orderReviewDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return orderReviewDao.count(map);
	}
	
	@Override
	public int save(OrderReviewDO orderReview){
		return orderReviewDao.save(orderReview);
	}
	
	@Override
	public int update(OrderReviewDO orderReview){
		return orderReviewDao.update(orderReview);
	}
	
	@Override
	public int remove(String id){
		return orderReviewDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return orderReviewDao.batchRemove(ids);
	}

    @Override
    public List<OrderReviewDO> likeList(Map<String, Object> map){
        return orderReviewDao.likeList(map);
    }
    @Override
    public int likeListCount(Map<String, Object> map){
        return orderReviewDao.likeListCount(map);
    }

    @Override
    public R getOrderReviewList(Map<String, Object> paramMap) {
		PageHelperUtils.page(paramMap);
		List<Map<String,Object>> list = orderReviewDao.getOrderListByStatus(paramMap);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return R.ok().put("data",pageInfo);

    }

	@Override
	public R updateOrderReview(Map<String, Object> paramMap) {
		if(paramMap.get("status").toString().equals("1")){
			//拒绝
			OrderReviewDO orderReviewDO = orderReviewDao.get(paramMap.get("reviewId").toString());
			orderReviewDO.setStatus(2);
			orderReviewDO.setUpAt(new Date());
			orderReviewDao.update(orderReviewDO);
			return R.ok();
		}else{
			//通过
			OrderReviewDO orderReviewDO = orderReviewDao.get(paramMap.get("reviewId").toString());
			if(orderReviewDO.getLevel()==2){
				orderReviewDO.setStatus(KitUtil.randomNumber(7));
				orderReviewDO.setUpAt(new Date());
				orderReviewDao.update(orderReviewDO);
				return R.ok();
			}else if(orderReviewDO.getLevel()==1){
				orderReviewDO.setStatus(1);
				orderReviewDO.setUpAt(new Date());
				orderReviewDao.update(orderReviewDO);
				orderReviewDO.setId(UuidUtil.get32UUID());
				MallCompanyUserDO byUserId = mallCompanyUserDao.getByUserId(paramMap.get("userId").toString());
				MallCompanyUserDO mallCompanyUserDO = mallCompanyUserDao.get(byUserId.getParentId());
				orderReviewDO.setUserId(mallCompanyUserDO.getUserId());
				orderReviewDO.setLevel(2);
				orderReviewDO.setStatus(0);
				orderReviewDao.save(orderReviewDO);
				return R.ok();
			}else{
				return R.error();
			}
		}

	}

	@Override
	public R deleteOrderReview(Map<String, Object> paramMap) {
		OrderReviewDO orderReviewDO = orderReviewDao.get(paramMap.get("reviewId").toString());
		orderReviewDO.setDelFlag(1);
		orderReviewDO.setUpAt(new Date());
		orderReviewDao.update(orderReviewDO);
		return R.ok(0,"删除成功");
	}



}
